<template>
  <a-card :bordered='false'>
    <div class='table-page-search-wrapper' style='margin-bottom: 10px'>
<!--      查询区域-->
      <a-form :form='form' layout='inline'>
        <a-row :gutter='12'>
          <a-col :md='4' :sm='8'>
            <a-form-item  label='所属项目'>
              <a-select  label-in-value :show-search="showSearch" placeholder='请输入项目' :filter-option="filterOption" v-decorator="['projectName',{}]" style='margin-right: 20px'  @change='initAreaList'>
                <a-select-option value="">请选择项目</a-select-option>
                <a-select-option v-for='item in projectList' :label='item.name' :value='item.id' :key='item.id'>
                  <span style="float: left">{{item.name}}</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='所在位置'>
              <a-input placeholder='所在位置模糊查询' v-model='queryParam.locateName'></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="10" :sm='8'>
            <a-form-item label="创建时间" :labelCol="{span: 5}" :wrapperCol="{span: 18}">
              <j-date v-model="queryParam.createTime_begin" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" style="width:45%" placeholder="请选择开始时间" ></j-date>
              <span style="width: 10px;">~</span>
              <j-date v-model="queryParam.createTime_end" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" style="width:45%" placeholder="请选择结束时间"></j-date>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <span style='float:left; overflow: hidden;' class='table-page-search-subbimButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a-button type='primary' @click='add' style='margin-left: 10px'>新增</a-button>
              <a-button type='primary' @click='deleteBatch' style='margin-left: 10px'>批量删除</a-button>
            </span>
        </a-row>
      </a-form>
    </div>
      <Build-modal-form @ok='ok' @modalFormOk='ok'  ref='buildModalForm'></Build-modal-form>
    <a-table
      ref='table'
      bordered
      size="middle"
      rowKey="id"
      :columns='columns'
      :dataSource='dataSource'
      :pagination='ipagination'
      :loading='loading'
      :rowSelection='{selectedRowKeys:selectedRowKeys,onChange:onSelectChange}'
      @change='handleTableChange'
      >
      <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定删除吗?" @confirm="() => deleteRecord(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
    </a-table>
  </a-card>

</template>
<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterObj } from '@/utils/util'
import { deleteAction, getAction, postAction } from '@api/manage'
import BuildModalForm from './modules/BuildModalForm'
import { getProjectList } from '@api/api'
export default {
  name:'BuildingList',
  mixins:[JeecgListMixin],
  components:{
    BuildModalForm
  },
  data(){
    return{
      form: this.$form.createForm(this),
      projectList:[],
      areaList:[],
      buildList:[],
      unitList:[],
      floorList:[],
      columns:[
        {
          title:'序号',
          dataIndex:'',
          key:'rowIndex',
          width:60,
          align:'center',
          customRender:function(t,r,index){
            return parseInt(index)+1
          }
        },
        {
          title:'所属项目',
          align:'center',
          dataIndex:'projectName',
        },
        {
          title:'门牌号',
          align:'center',
          dataIndex:'houseNum'
        },
        {
          title:'所处位置',
          align:'center',
          dataIndex:'locateName'
        },
        {
          title:'户主',
          align:'center',
          dataIndex: 'hostName'
        },
        {
          title:'住户数',
          align: 'center',
          dataIndex:'peopleNum'
        },
        {
          title:'创建时间',
          align:'center',
          dataIndex:'createDateTime',
        },
        {
          title:'操作',
          align: 'center',
          dataIndex:'action',
          scopedSlots:{
            customRender:'action'
          }
        }
      ],
      showSearch:true,
      url:{
        list:'/building/buildingVoList',
        deleteRecord:'/house/delHouse',
        deleteBatch:'/house/deleteBatch',
        getAreaInfos:'/area/getAreaList',
        getbuildInfos:'/building/getBuildList',
        getUnitInfos:'/unit/getUnitList',
        getFloorInfos:'floor/getFloorList',
        addHouse:'house/addHouse'
      },
      queryParam:{
        "areaName":"",
        "projectName":"",
        "buildName":"",
        "unitName":"",
        "floorName":"",
        "hostName":"",
      },
    }
  },
  created(){
    this.keyupEnter()
    this.initProjectList()
  },
  methods:{
    searchReset(){
      this.form.resetFields()
      this.areaList=[]
      this.buildList=[]
      this.unitList=[]
      this.floorList=[]
      this.queryParam={}
      this.loadData(1)
    },
    initProjectList() {
      getProjectList().then((res)=>{
        if(res.success){
          this.projectList = res.result
        }
      })
    },
    initAreaList(e) {
      this.queryParam.projectName = e.label[0].children[0].text
      let params = {
        projectId:e.key
      }
      this.form.resetFields(['areaName','buildName','unitName','floor'])
      getAction(this.url.getAreaInfos,params).then((res)=>{
        if(res.success){
          this.areaList = res.result
        }
      })
    },

    initBuildList(e) {
      this.queryParam.areaName = e.label[0].children[0].text
      this.form.resetFields(['buildName','unitName','floor'])
      let params = {
        areaId:e.key
      }
      getAction(this.url.getbuildInfos,params).then((res)=>{
        if(res.success){
          this.buildList = res.result
        }
      })
    },
    initUnitList(e) {
      this.queryParam.buildName = e.label[0].children[0].text
      this.form.resetFields(['unitName','floor'])
      let params = {
        buildingId:e.key
      }
      getAction(this.url.getUnitInfos,params).then((res)=>{
        if(res.success){
          this.unitList = res.result

        }
      })
    },
    initFloorList(e) {
      this.queryParam.unitName = e.label[0].children[0].text
      this.form.resetFields(['floor'])
      let params = {
        unitId:e.key
      }
      this.floorId=""
      getAction(this.url.getFloorInfos,params).then((res)=>{
        if(res.success){
          this.floorList = res.result
        }
      })
    },
    setFloorId(e){
      this.queryParam.floorName = e.label[0].children[0].text
      this.floorId=e.key
    },
    filterOption(input, option) {
      let com = option.componentOptions
      let name = String(com.propsData.label)
      if(name.toLowerCase().includes(input.toLowerCase())){
        return true
      }
    },
    deleteRecord(record){
      deleteAction(this.url.deleteRecord, { id:record.id }).then(res =>{
        if (res.success){
          this.$message.success(res.message)
          this.loadData();
        }else {
          this.$message.warning(res.message)
        }
      })
    },
    loadData(arg){
      if (arg === 1) {
        this.ipagination.current = 1
      }
      let params = this.getQueryParams()
      this.loading=true
      getAction(this.url.list,params).then(res=>{
        if(res.success&&res.result!=null){
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code==510){
          this.$message.warning(res.message)
        }

      })
      this.loading = false
    },
    add(){
      this.$refs.buildModalForm.title="新增";
      this.$refs.buildModalForm.visible=true;
      this.$refs.buildModalForm.disableSubmit=false;
    },
    deleteBatch(){
      if (this.selectedRowKeys.length<=0){
        this.$message.warning('请选择一条记录')
        return
      }else{
        var ids=""
        for (let i = 0; i < this.selectedRowKeys.length; i++) {
          ids+=this.selectionRows[i].id+','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function() {
            deleteAction(that.url.deleteBatch, { ids: ids }).then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData(1)
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }

    },
    exportXls(){

    },
    searchQuery(){
      let startTime = this.queryParam.createTime_begin;
      let endTime  = this.queryParam.createTime_end;
      if (startTime != "" && endTime != "") {
        if (startTime > endTime) {
          this.$message.error("开始时间不能大于结束时间");
          return
        }
      }
      if (startTime == "" && endTime != "") {
        this.$message.error("请选择开始时间");
        return
      }
      if (startTime != "" && endTime == "") {
        this.$message.error("请选择结束时间");
        return
      }
      this.loadData(1)
    },
    ok(){
      this.loadData(1)
    },
    getQueryParams(){
      let param = Object.assign({},this.isorter,this.queryParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      param.areaName = this.queryParam.areaName
      param.createUser =  this.queryParam.createUser
      param.locateName = this.queryParam.locateName
      if (this.queryParam.createTime_begin != "" && this.queryParam.createTime_end != "") {
        param.startTime1 = this.queryParam.createTime_begin + " 00:00:00";
        param.endTime1 = this.queryParam.createTime_end + " 23:59:59";
      }
      return filterObj(param)
    },
    keyupEnter() {
      document.onkeydown = e => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1)
        }
      }
    },
  }
}
</script>